﻿
package extremefx.tools {
	import extremefx.tools.getClassByName;		

	/**
	 * @author Marcelo Volmaro
	 */
	public final class TypeConverter {
		/**
	     * Converts a variable from a String to the best suited type for the variable.
	     *
	     * @param value the value to convert
	     * @return the converted value, or the input string if a conversion was not possible.
	     */
		public static function convert(value:String):* {
			if (value == "true") return true;
			if (value == "false") return false;
			
			if (!isNaN(Number(value))) return Number(value);
			
			var result:Object =  getClassByName(value);
			if (result != null) return result;
			
			if (value.indexOf(".") != -1) {
				var a:Array = value.split(".");
				var last:String = String(a.pop());
				
				result = getClassByName(a.join("."));
				
				if (result != null && result.hasOwnProperty(last)) {
					return result[last];
				}
			}
			
			return value;
		}
	}
}
